﻿<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <!--全局通用框架样式 begin-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--/*@thymesVar id="title" type=""*/-->
    <title>汽车信息</title>
    <link href="/coral/images/favicon.ico" rel="icon">
    <link href="/assets/libs/layui/css/layui.css" rel="stylesheet"/>
    <link href="/assets/module/admin.css" rel="stylesheet">

    <link href="//cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js"></script>
    <script type="text/javascript" src="/coral/js/custom.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <!--                <div class="layui-form-item">-->
                <!--                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">车牌号:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="carNumber" class="layui-input" placeholder="请输入车牌号"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">罚款:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="fine" class="layui-input" placeholder="请输入罚款"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">记分:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="fraction" class="layui-input" placeholder="请输入记分"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">违章行为:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="behavior" class="layui-input" placeholder="请输入违章行为"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">违章地址:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="address" class="layui-input" placeholder="请输入违章地址"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">违章代码:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="code" class="layui-input" placeholder="请输入违章代码"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">违章城市:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="city" class="layui-input" placeholder="请输入违章城市"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">处理编号:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="number" class="layui-input" placeholder="请输入处理编号"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="layui-inline">-->
                <!--                                        <label class="layui-form-label">状态:</label>-->
                <!--                                        <div class="layui-input-inline">-->
                <!--                                            <input name="state" class="layui-input" placeholder="请输入状态"/>-->
                <!--                                        </div>-->
                <!--                                    </div>-->
                <!--                                                                                                                            <div class="layui-inline">&emsp;-->
                <!--                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>-->
                <!--                            <i class="layui-icon">&#xe615;</i>搜索-->
                <!--                        </button>&nbsp;-->
                <!--                        <button shiro:hasPermission="violation:export" id="exportBtn" class="layui-btn icon-btn" type="button">-->
                <!--                            <i class="layui-icon">&#xe67d;</i>导出-->
                <!--                        </button>-->
                <!--                    </div>-->
                <!--                </div>-->
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">车牌号:</label>
            <div class="layui-input-block">
                <!--                <input name="carNumber" placeholder="请输入车牌号" class="layui-input"-->
                <!--                       lay-verType="tips" lay-verify="required" required/>-->
                <select name="carNumber"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">罚款:</label>
            <div class="layui-input-block">
                <input name="fine" placeholder="请输入罚款" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">记分:</label>
            <div class="layui-input-block">
                <input name="fraction" placeholder="请输入记分" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">违章时间:</label>
            <div class="layui-input-block">
                <input id="time" name="time" placeholder="请选择违章时间"
                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">违章行为:</label>
            <div class="layui-input-block">
                <input name="behavior" placeholder="请输入违章行为" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">违章地址:</label>
            <div class="layui-input-block">
                <input name="address" placeholder="请输入违章地址" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">违章代码:</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入违章代码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">违章城市:</label>
            <div class="layui-input-block">
                <input name="city" placeholder="请输入违章城市" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">处理编号:</label>
            <div class="layui-input-block">
                <input name="number" placeholder="请输入处理编号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">创建时间:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input id="creDate" name="creDate" placeholder="请选择创建时间"-->
        <!--                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"-->
        <!--                       lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">状态:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="state" placeholder="请输入状态" class="layui-input"-->
        <!--                       lay-verType="tips" lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'carNumber', title: '车牌号', align: 'center'},
            {field: 'fine', title: '罚款', align: 'center'},
            {field: 'fraction', title: '记分', align: 'center'},
            {field: 'behavior', title: '违章行为', align: 'center'},
            {field: 'address', title: '违章地址', align: 'center'},
            {field: 'code', title: '违章代码', align: 'center'},
            {field: 'city', title: '违章城市', align: 'center'},
            {field: 'number', title: '处理编号', align: 'center'},
            {field: 'time', title: '违章时间', align: 'center'},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: '/violation/selViolationPageAfter',
            page: true,
            toolbar: [shiroToolbar(true, true)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            method: 'post',
            dataType: 'json',
            contentType: 'application/json',
            headers: {"X-Token": C.find("X-Token")},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.errorCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.body.total, //解析数据长度
                    "data": res.body.records //解析数据列表
                };
            },
            request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(true, true));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据


                    $.ajax({
                        url: "/car/selCarList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择车辆</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.carNumber == tp.carNumber) {
                                            html += "<option value='" + tp.carNumber + "'  selected='selected'>" + tp.carNumber + "</option>"
                                        } else {
                                            html += "<option value='" + tp.carNumber + "'>" + tp.carNumber + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.carNumber + "'>" + tp.carNumber + "</option>"
                                    }
                                }
                                $("[name='carNumber']").html("");
                                $("[name='carNumber']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })
                    form.on('submit(saveBtn)', function (data) {

                        var carNumber = $("[name='carNumber']").val();
                        var fine = $("[name='fine']").val();
                        var fraction = $("[name='fraction']").val();
                        var time = $("[name='time']").val();
                        var behavior = $("[name='behavior']").val();
                        var address = $("[name='address']").val();
                        var code = $("[name='code']").val();
                        var city = $("[name='city']").val();
                        var number = $("[name='number']").val();


                        if (mData) {

                            var data = {
                                "id": mData.id,
                                "carNumber": carNumber,
                                "fine": fine,
                                "fraction": fraction,
                                "time": time,
                                "behavior": behavior,
                                "address": address,
                                "city": city,
                                "code": code,
                                "number": number
                            }
                            $.ajax({
                                url: '/violation/update',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('修改成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        } else {
                            var data = {
                                "carNumber": carNumber,
                                "fine": fine,
                                "fraction": fraction,
                                "time": time,
                                "behavior": behavior,
                                "address": address,
                                "city": city,
                                "code": code,
                                "number": number
                            }

                            $.ajax({
                                url: '/violation/insert',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('添加成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        }
                        // }
                        return false;
                    });
                }
            });

            /* 渲染laydate */
            laydate.render({
                elem: '#time', type: 'datetime'
            });
            /* 渲染laydate */
            laydate.render({
                elem: '#creDate', type: 'datetime'
            });
        }

        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = '';
                data.field.endDate = '';
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                $.ajax({
                    url: '/violation/delete?id=' + obj.id,
                    type: 'post',
                    // data: {"id": obj.id},
                    dataType: 'JSON',
                    contentType: 'application/json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("X-Token", C.find("X-Token"));
                    },
                    success: function (res) {
                        if (res.message == 'success') {
                            layer.msg('删除成功', {icon: 1})
                            // layer.close(dIndex);
                            // show();
                            insTb.reload({page: {curr: 1}});

                        } else {
                            layer.msg(res.message);
                        }

                    }
                })
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
    });
</script>
</body>
</html>